#include "string.h"
#include "delay.h"
#include "usart1.h"
#include "led_c13.h"
#include "ds3231.h"
#include "enc28j60.h"
#include "arp.h"
#include "ip.h"
#include "ethernet2.h"

#include "stm32f10x.h"

#ifndef NULL
#define NULL			((void *)0)
#endif

#if 0
static void ds3231_get_time(void)
{
	unsigned char time[32] = {0};
	ds3231_get(time, 3);
	usart1_send_string("\r\n\r\n");
	usart1_send_dec(time[2]);
	usart1_send_string(":");
	usart1_send_dec(time[1]);
	usart1_send_string(":");
	usart1_send_dec(time[0]);
	usart1_send_string("\r\n");
}

static void ds3231_set_time(unsigned char sec, unsigned char min, unsigned char hou)
{
	unsigned char time[32] = {0};
	time[0] = sec;
	time[1] = min;
	time[2] = hou;
	ds3231_set(time, 3);
}
#endif

static void enc28j60_get_info(ENC28J60_INTERFACE_P obj)
{
	USART1_INTERFACE_P usart1 = NULL;

	usart1_get_interface(&usart1);

	usart1->send_string("ENC28J60_EREVID:\t");
	usart1->send_int(obj->read(ENC28J60_EREVID), 1);
	usart1->send_string("\r\n");
	usart1->send_string("MAC ADDRESS:\t\t");
	usart1->send_int(obj->read(ENC28J60_MAADR0), 2);
	usart1->send_char(':');
	usart1->send_int(obj->read(ENC28J60_MAADR1), 2);
	usart1->send_char(':');
	usart1->send_int(obj->read(ENC28J60_MAADR2), 2);
	usart1->send_char(':');
	usart1->send_int(obj->read(ENC28J60_MAADR3), 2);
	usart1->send_char(':');
	usart1->send_int(obj->read(ENC28J60_MAADR4), 2);
	usart1->send_char(':');
	usart1->send_int(obj->read(ENC28J60_MAADR5), 2);
	usart1->send_string("\r\n");
}

int main(void)
{
	//USART1_INTERFACE_P usart1 = NULL;
	ENC28J60_INTERFACE_P enc28j60 = NULL;
	//IP_INTERFACE_P ip = NULL;
	//ARP_INTERFACE_P arp = NULL;
	//ETHERNET2_INTERFACE_P ethernet = NULL;

	int recv = 0;
	
	//unsigned char send_buf[256];
	unsigned char recv_buf[256];
	unsigned char my_mac_addr[] = {0x74, 0xD4, 0x35, 0xFA, 0x83, 0xB4};
	//unsigned char broadcase_addr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
	//unsigned char zero_addr[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};

	//unsigned char host_addr1[] = {0x74, 0xD4, 0x35, 0xFA, 0x83, 0xB3};
	//unsigned char host_addr2[] = {0x0C, 0x82, 0x68, 0x66, 0x20, 0x28};

	// init usart1
	//usart1_init();

	// init ds3231
	//init_ds3231();

	// init led
	led_c13_init(0x1);

	// init enc28j60
	enc28j60_init(my_mac_addr);

	// init arp
	//arp_init(my_mac_addr, "192.168.41.225");

	// init ip
	//ip_init("192.168.41.225");

	// init ethernet2
	//ethernet2_init(my_mac_addr);

	//usart1_get_interface(&usart1);
	enc28j60_get_interface(&enc28j60);
	//arp_get_interface(&arp);
	//ip_get_interface(&ip);
	//ethernet2_get_interface(&ethernet);

	//enc28j60_get_info(enc28j60);

	//arp->alloc(arp, 0x2, host_addr1, "192.168.41.1");
	//arp->tobuf(arp, send_buf, sizeof(send_buf));

	//ethernet->alloc(ethernet, host_addr1, ETHERNET2_TYPE_ARP, send_buf, arp->size);
	//ethernet->tobuf(ethernet, send_buf, sizeof(send_buf));

	while(1)
	{
		delay_ms(50);
		//led_c13_swc();
		recv = enc28j60->recv(recv_buf, sizeof(recv_buf));
		if(0 < recv)
		{
			//enc28j60->send(send_buf, ethernet->size);
			//usart1->send_string("--------------->recv = ");
			//usart1->send_int(recv, 1);
			//usart1->send_string("<---------------\r\n");
			led_c13_swc();
		}
	}
}